package de.acagamics.roteschlupfmuetze.level;

import java.util.Random;

import com.golden.gamedev.object.Sprite;

import de.acagamics.roteschlupfmuetze.RoteSchlupfmuetze;

public class Cloud {
	Sprite sprite = new Sprite(RoteSchlupfmuetze.loadImage(
			"de/acagamics/roteschlupfmuetze/dummy/cloud_sample.png"));
	
	public static int MIN_HEIGHT = 50;
	public static int MAX_HEIGHT = 200;
	
	public static int MIN_SPEED = 1;
	public static int MAX_SPEED = 5;
	
	int DISTANCE;
	
	double SPEED = 0.5;
	
	Random random = new Random();
	
	double time;
	
	public Cloud(int i, int clouds) {
		DISTANCE = RoteSchlupfmuetze.WIDTH + sprite.getWidth();
		reset();
		time = i * (RoteSchlupfmuetze.WIDTH / clouds);
		sprite.setX(RoteSchlupfmuetze.WIDTH - time);
	}

	public Sprite getSprite() {
		return sprite;
	}
	
	public void update(long elapsedTime) {
		if (time >= DISTANCE)
			reset();
		else
			time += elapsedTime * SPEED;
	}
	
	private void reset() {
		SPEED = (double)(Cloud.MIN_SPEED + random.nextInt(Cloud.MAX_SPEED - Cloud.MIN_SPEED + 1)) / 10;
		sprite.forceX(RoteSchlupfmuetze.WIDTH);
		sprite.setHorizontalSpeed(-SPEED);
		setY();
		time = 0;
	}
	
	private void setY() {
		sprite.forceY(random.nextInt(Cloud.MAX_HEIGHT) - sprite.getHeight() + Cloud.MIN_HEIGHT);
	}
}
